<?php

/**
 *  学校和家长互动
 */

class Controller_interaction extends MyApp {


    /** 首页 **/
    public function actionIndex()
    {

         $this->tpl->display();
    }
	
	/** 学校发布通知列表 **/
	public function actionInteractionTeacherList ()
	{	
		// 部门列表
		$department_list= $this->db->fetchAll("SELECT `department_name`,`department_id` FROM `tbl_department` ");
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$interaction_lists = $this->db->fetchAll("SELECT * FROM `tbl_interaction` WHERE interaction_type=1 ORDER BY interaction_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
			
		// 搜索
		if($_POST){

			$interaction_title			= $this->post->interaction_title;
			$department_id				= $this->post->department_id;

			// 标题模糊搜索
			
			if(!empty($interaction_title)){
				
				$interaction_lists = $this->db->fetchAll("SELECT * FROM `tbl_interaction` WHERE `interaction_title` LIKE '%{$interaction_title}%' ORDER BY interaction_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
			}
			// 部门搜索
			if(!empty($department_id)){
				
				$interaction_lists = $this->db->fetchAll("SELECT * FROM `tbl_interaction` WHERE department_id ={$department_id} ORDER BY interaction_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

			}
			// 转发
			if(!empty($interaction_transfer)){
				$info = array(   
								
				"interaction_transfer"			=> $interaction_transfer,
				
				);
				$this->db->do_update("tbl_interaction", $info, "interaction_id='{$interaction_id}'");
				// 跳转
				Helper_Client::location("添加成功", "index.php?a=interaction.interactionlist");
				}
		}

		foreach($interaction_lists as $key=>$value){
			//	部门
			$department_data= $this->db->fetchRow("SELECT `department_name`,`department_id` FROM `tbl_department` WHERE `department_id`={$value['department_id']}");
			$interaction_lists[$key]['department_name']=$department_data['department_name'];
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("interaction_lists", $interaction_lists);
		$this->tpl->assign("department_list", $department_list);
		$this->tpl->display('interaction/Teacher.list.tpl.php');

	}
	/** 学校给家长发布通知、家长给学校发布内容 **/
	public function actionInteractionTeacherAdd()
    {   
		// 发布部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");
		// 通知样式
		$style_lists	  = $this->db->fetchAll("SELECT * FROM `tbl_inform_style`");
	
        if ($_POST){ 
            
			$department_id			= $this->post->department_id;
			$style_id				= $this->post->style_id;
			$interaction_title      = $this->post->interaction_title;
			$interaction_content    = $this->post->interaction_content;
			$interaction_time     	= $this->post->interaction_time;
			
			// 标题格式
			if($style_id =='请选择标题格式'){
				Helper_Client::alert("请选择标题格式");
			}

            // 写入数据库
            $info = array(   
                			
			"department_id"			=> $department_id,
			"style_id"				=> $style_id,
			"interaction_title"     => $interaction_title, 
			"interaction_content"	=> $interaction_content,
			"interaction_time"		=> $interaction_time,
			// interaction_type=1 是通知发布 =2是家长来信
			"interaction_type"		=> 1,
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  
			
            );
			$this->db->do_insert("tbl_interaction", $info);
            // 跳转
            Helper_Client::location("发布成功", "index.php?a=interaction.interactionteacherlist");
        }
       
      	$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("style_lists", $style_lists);
        $this->tpl->display('interaction/teacher.add.tpl.php');
    }
	/** 学校给家长发布通知修改 **/
    public function actionInteractionTeacherUpdate()
    {
		//选择进入家长发布，还是学校发布的渠道
		$interaction_type = (int)$this->get->interaction_type;
        $interaction_id   = (int)$this->get->interaction_id;
		// 通知
		$interaction_data = $this->db->fetchRow("SELECT * FROM `tbl_interaction` WHERE interaction_id={$interaction_id} ");
		// 发布部门
		$department_data = $this->db->fetchRow("SELECT `department_name` FROM `tbl_department` WHERE department_id={$interaction_data['department_id']} ");

		// 通知样式
		$style_list = $this->db->fetchAll("SELECT * FROM `tbl_inform_style` ");

        if ($_POST){
            
            $style_id	     	    = $this->post->style_id;
			$interaction_title	    = $this->post->interaction_title;
			$interaction_time	    = $this->post->interaction_time;
			$interaction_content	= $this->post->interaction_content;

             
            // 写入数据库
            $info = array(  
			
			"style_id"				=> $style_id,
			"interaction_title"     => $interaction_title, 
			"interaction_content"	=> $interaction_content,
			"interaction_time"		=> $interaction_time,
			"interaction_type"		=> $interaction_type,
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00", 
            				
            );
     
            $this->db->do_update("tbl_interaction", $info, "interaction_id='{$interaction_id}'");
      
            // 跳转
            Helper_Client::location("修改成功", "index.php?a=interaction.interactionteacherlist");
        }

		$this->tpl->assign("style_list", $style_list);

        $this->tpl->assign("interaction_data", $interaction_data);
		$this->tpl->assign("department_data", $department_data);
		$this->tpl->assign("department_lists", $department_lists);
        $this->tpl->display('interaction/teacher.update.tpl.php');
    }
	/** 学校发给家长通知删除 **/
    public function actionInteractionTeacherDel()
    {
        $interaction_id = $this->get->interaction_id;   
        $this->db->query("DELETE  FROM `tbl_interaction` WHERE interaction_id='{$interaction_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=interaction.interactionteacherlist");
    }
	/** 学校发给家长通知详情 **/
	public function actionTeacherParticulars()
    {	

        $interaction_id   = (int)$this->get->interaction_id;
		// 通知
		$interaction_data = $this->db->fetchRow("SELECT * FROM `tbl_interaction` WHERE interaction_id={$interaction_id} ");
		// 发布部门
		$department_data = $this->db->fetchRow("SELECT `department_name` FROM `tbl_department` WHERE department_id={$interaction_data['department_id']} ");

		// 通知样式
		$style_data = $this->db->fetchRow("SELECT * FROM `tbl_inform_style` WHERE style_id={$interaction_data['style_id']} ");
        
		$this->tpl->assign("style_data", $style_data);
        $this->tpl->assign("interaction_data", $interaction_data);
		$this->tpl->assign("department_data", $department_data);
        $this->tpl->display('interaction/teacher.particulars.tpl.php');
    }

	/** ----------------------------------家长来信------------------------------------------ **/
	/** 家长来信列表 **/
	public function actionInteractionStudentList ()
	{	
		
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$interaction_lists = $this->db->fetchAll("SELECT * FROM `tbl_interaction` WHERE interaction_type=2 ORDER BY interaction_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		
		// 搜索
		if($_POST){

			$interaction_title			= $this->post->interaction_title;
			$department_id				= $this->post->department_id;

			// 标题模糊搜索
			
			if(!empty($interaction_title)){
				
				$interaction_lists = $this->db->fetchAll("SELECT * FROM `tbl_interaction` WHERE `interaction_title` LIKE '%{$interaction_title}%' ORDER BY interaction_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
			}
			// 部门搜索
			if(!empty($department_id)){
				
				$interaction_lists = $this->db->fetchAll("SELECT * FROM `tbl_interaction` WHERE department_id ={$department_id} ORDER BY interaction_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

			}
			// 转发
			if(!empty($interaction_transfer)){
				$info = array(   
								
				"interaction_transfer"			=> $interaction_transfer,
				
				);
				$this->db->do_update("tbl_interaction", $info, "interaction_id='{$interaction_id}'");
				// 跳转
				Helper_Client::location("添加成功", "index.php?a=interaction.interactionlist");
				}
		}

		foreach($interaction_lists as $key=>$value){
			// 学生信息
			$student_data = $this->db->fetchRow("SELECT * FROM `tbl_student` WHERE student_id={$value['student_id']}");
			$interaction_lists[$key]['student_pname'] = $student_data['student_pname'];
			$interaction_lists[$key]['student_ptel'] = $student_data['student_ptel'];
			$interaction_lists[$key]['student_relation'] = $student_data['student_relation'];
			$interaction_lists[$key]['student_name'] = $student_data['student_name'];
			// 班级
			$class_data = $this->db->fetchRow("SELECT * FROM `tbl_class` WHERE class_id={$student_data['class_id']}");
			$interaction_lists[$key]['class_name'] = $class_data['class_name'];
				
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("interaction_lists", $interaction_lists);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("student_data", $student_data);
		$this->tpl->display('interaction/student.list.tpl.php');

	}
	/** 家长发给学校信件详情 **/
	public function actionStudentParticulars()
    {	

        $interaction_id   = (int)$this->get->interaction_id;
		// 来信
		$interaction_data = $this->db->fetchRow("SELECT * FROM `tbl_interaction` WHERE interaction_id={$interaction_id} ");		

		// 家长姓名
		$student_data = $this->db->fetchRow("SELECT `student_pname` FROM `tbl_student` WHERE student_id={$interaction_data['student_id']}");
							
		// 通知样式
		$style_data = $this->db->fetchRow("SELECT * FROM `tbl_inform_style` WHERE style_id={$interaction_data['style_id']} ");
        
		$this->tpl->assign("style_data", $style_data);
		$this->tpl->assign("student_data", $student_data);
        $this->tpl->assign("interaction_data", $interaction_data);
        $this->tpl->display('interaction/student.particulars.tpl.php');
    }
	/** 学校给家长的回信 **/
	public function actionStudentReply()
    {	

        $interaction_id		= (int)$this->get->interaction_id;
		// 来信
		if ($_POST){
            
            $interaction_reply	    = $this->post->interaction_reply;
			            
            // 写入数据库
            $info = array(  
			
			"interaction_reply"		=> $interaction_reply,
            "status"                => 1,
            "reply_time"            => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00", 
            				
            );
     
            $this->db->do_update("tbl_interaction", $info, "interaction_id='{$interaction_id}'");
      
            // 跳转
            Helper_Client::location("修改成功", "index.php?a=interaction.interactionstudentlist");
        }
        
        $this->tpl->display('interaction/student.reply.tpl.php');
    }

}
	
?>